package com.powernode.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.powernode.domain.Order;
import com.powernode.dto.PayOrderDto;
import com.powernode.service.OrderService;
import com.powernode.vo.OrderConfirm;
import com.powernode.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/p/myOrder")
@RestController
@Api(tags = "订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/confirm")
    @ApiOperation("提交订单")
    public ResponseEntity<OrderVo> confirm(@RequestBody OrderConfirm orderConfirm) {
        OrderVo orderVo = orderService.confirm(orderConfirm);
        return ResponseEntity.ok(orderVo);
    }

    @PostMapping("/submit")
    @ApiOperation("提交订单 生成订单")
    public ResponseEntity<String> submit(@RequestBody OrderVo orderVo) {
        String orderNo = orderService.submit(orderVo);
        return ResponseEntity.ok(orderNo);
    }

    @PostMapping("/loadOrderByNo")
    @ApiOperation("订单号获取订单")
    public Order loadOrderByNo(@RequestBody String orderNumber) {
        return orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, "xxx:" + orderNumber));
    }

    @PostMapping("/updateOrder")
    @ApiOperation("根据付款状态修改订单")
    public void updateOrder(@RequestBody PayOrderDto orderDto) {
        orderService.update(new UpdateWrapper<Order>()
                .eq("order_number", orderDto.getOrderNo())
                .set("status", orderDto.getOrderStatus())
                .set(!ObjectUtils.isEmpty(orderDto.getOrderType()), "close_type", orderDto.getOrderType()));
    }
}
